package src.epat.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import src.epat.mapper.TaskMapper;
import src.epat.model.TaskModel;

import java.util.List;

@Service
public class TaskService {

    @Autowired
    TaskMapper taskMapper;


    public void addTask(TaskModel taskModel) {
        taskModel.setFinishIndex(taskModel.getOriginIndex() - 1);
        taskModel.setIsSuccess(true);
        taskModel.setIsDel(false);
        taskMapper.insert(taskModel);
    }

    public void updateTask(TaskModel taskModel) {
        taskMapper.updateById(taskModel);
    }

    public void finishTask(Long taskId, Boolean flag) {
        TaskModel taskModel = taskMapper.selectById(taskId);
        taskModel.setIsSuccess(flag);
        if (flag) {
            taskModel.setFinishIndex(taskModel.getFinishIndex() + 1);
        }
        taskMapper.updateById(taskModel);
    }

    public void finishTaskList(List<Long> taskIdList, Boolean flag) {
        for (Long taskId : taskIdList) {
            finishTask(taskId, flag);
        }
    }

    public List<TaskModel> listToDo(Integer envIndex, String searchName, Boolean isSuccess) {
        LambdaQueryWrapper<TaskModel> taskModelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        taskModelLambdaQueryWrapper.eq(TaskModel::getFinishIndex, envIndex - 1);
        taskModelLambdaQueryWrapper.eq(TaskModel::getIsSuccess, isSuccess);
        taskModelLambdaQueryWrapper.like(isNotEmpty(searchName), TaskModel::getName, searchName);
        taskModelLambdaQueryWrapper.and(wrapper1 -> {
            wrapper1.eq(TaskModel::getIsTransfer, true);
            wrapper1.or(wrapper2 -> {
                wrapper2.eq(TaskModel::getOriginIndex, envIndex);
            });
        });
        taskModelLambdaQueryWrapper.eq(TaskModel::getIsDel, false);
        taskModelLambdaQueryWrapper.orderByDesc(TaskModel::getCreateTime);
        return taskMapper.selectList(taskModelLambdaQueryWrapper);
    }

    public List<TaskModel> listFinish(Integer envIndex, String searchName) {
        LambdaQueryWrapper<TaskModel> taskModelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        taskModelLambdaQueryWrapper.ge(TaskModel::getFinishIndex, envIndex);
        taskModelLambdaQueryWrapper.le(TaskModel::getOriginIndex, envIndex);
        taskModelLambdaQueryWrapper.like(isNotEmpty(searchName), TaskModel::getName, searchName);
        taskModelLambdaQueryWrapper.orderByDesc(TaskModel::getCreateTime);
        taskModelLambdaQueryWrapper.eq(TaskModel::getIsDel, false);
        return taskMapper.selectList(taskModelLambdaQueryWrapper);
    }


    public List<TaskModel> listDel(Integer envIndex, String searchName) {
        LambdaQueryWrapper<TaskModel> taskModelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        taskModelLambdaQueryWrapper.eq(TaskModel::getFinishIndex, envIndex - 1);
        taskModelLambdaQueryWrapper.eq(TaskModel::getIsSuccess, true);
        taskModelLambdaQueryWrapper.like(isNotEmpty(searchName), TaskModel::getName, searchName);
        taskModelLambdaQueryWrapper.and(wrapper1 -> {
            wrapper1.eq(TaskModel::getIsTransfer, true);
            wrapper1.or(wrapper2 -> {
                wrapper2.eq(TaskModel::getOriginIndex, envIndex);
            });
        });
        taskModelLambdaQueryWrapper.eq(TaskModel::getIsDel, true);
        taskModelLambdaQueryWrapper.orderByDesc(TaskModel::getCreateTime);
        return taskMapper.selectList(taskModelLambdaQueryWrapper);
    }

    public void delList(List<Long> taskIdList) {
        LambdaUpdateWrapper<TaskModel> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.in(TaskModel::getId , taskIdList);
        TaskModel taskModel = new TaskModel();
        taskModel.setIsDel(true);
        taskMapper.update(taskModel, queryWrapper);
    }

    public Boolean isNotEmpty(String text) {
        return text != null && text.length() > 0;
    }

}
